import i18n from 'i18next'

/**
 * action
 */
export const CHANGE_LANGUAGE = 'change_language'
export const changeLanguageActionCreator = (languageCode: 'zh' | 'en') => {
	return {
		type: CHANGE_LANGUAGE,
		payload: languageCode
	}
}
export type ChangeLanguageAction = ReturnType<typeof changeLanguageActionCreator>

/**
 * reducer
 */
const languageReducer = (state = 'zh', action: ChangeLanguageAction) => {
	switch (action.type) {
		case CHANGE_LANGUAGE:
			i18n.changeLanguage(action.payload) // 不标准，有副作用
			return action.payload
		default:
			return state
	}
}
export default languageReducer
